package com.gitee.ly.ymorm.mapping;

import com.gitee.ly.ymorm.YmOrmConfig;
import com.gitee.ly.ymorm.log.YmOrmLog;
import com.gitee.ly.ymorm.log.YmOrmLogFactory;

/**
 * YmOrmStatement
 *
 * @author ly
 **/
public final class YmOrmStatement {
    private YmOrmConfig configuration;
    private String id;
    private SqlSource sqlSource;
    private SqlCommandType sqlCommandType;
    private YmOrmLog log;
    private StatementType statementType;
    private Integer fetchSize;
    private Integer timeout;

    public YmOrmStatement() {
    }

    public static class Builder {
        private final YmOrmStatement ymOrmStatement = new YmOrmStatement();

        public Builder(YmOrmConfig configuration, String id, SqlSource sqlSource, SqlCommandType sqlCommandType) {
            ymOrmStatement.configuration = configuration;
            ymOrmStatement.id = id;
            ymOrmStatement.sqlSource = sqlSource;
            ymOrmStatement.sqlCommandType = sqlCommandType;
            String logId = id;
            if (configuration.getLogPrefix() != null) {
                logId = configuration.getLogPrefix() + logId;
            }
            ymOrmStatement.log = YmOrmLogFactory.getLog(logId);
        }

        public Builder statementType(StatementType statementType) {
            ymOrmStatement.statementType = statementType;
            return this;
        }

        public Builder fetchSize(Integer fetchSize) {
            ymOrmStatement.fetchSize = fetchSize;
            return this;
        }

        public Builder timeout(Integer timeout) {
            ymOrmStatement.timeout = timeout;
            return this;
        }

        public YmOrmStatement build() {
            assert ymOrmStatement.configuration != null;
            assert ymOrmStatement.id != null;
            assert ymOrmStatement.sqlSource != null;
            assert ymOrmStatement.sqlCommandType != null;
            assert ymOrmStatement.log != null;
            return ymOrmStatement;
        }
    }

    public YmOrmConfig getConfiguration() {
        return configuration;
    }

    public YmOrmLog getLog() {
        return log;
    }

    public String getId() {
        return id;
    }

    public SqlSource getSqlSource() {
        return sqlSource;
    }

    public SqlCommandType getSqlCommandType() {
        return sqlCommandType;
    }

    public StatementType getStatementType() {
        return statementType;
    }

    public Integer getFetchSize() {
        return fetchSize;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public BoundSql getBoundSql(ParamMap parameterObject) {
        BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
        return boundSql;
    }
}
